import logging
from django import forms
from google.appengine.ext.db import djangoforms
from player.models import Player



class PlayerRegisterForm(forms.ModelForm):
        
    email = forms.EmailField(label='Email')
    first_name = forms.CharField(label='Nombre')    
    pass1 = forms.CharField(label='Pass',widget= forms.PasswordInput)
    pass2 = forms.CharField(label='Pass (repetir)',widget= forms.PasswordInput)        

    def clean_pass1(self):
        if len(self.cleaned_data.get('pass1')) < 3:
            raise forms.ValidationError("La clave debe tener al menos 3 caracteres")
        return self.cleaned_data.get('pass1')
    
    def clean_pass2(self):
        cd = self.cleaned_data    
        if cd.get('pass1') != cd.get('pass2'):
            raise forms.ValidationError("Las claves son diferentes")
        return cd.get('pass1')
    

    def clean_email(self):
        if Player.all().filter('email =',self.cleaned_data.get('email')).get():
            raise forms.ValidationError("Existe un usuario con ese email")
        return self.cleaned_data.get('email')
    